#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!\n"
    exit 1
fi
clear

mysqlrootuser="root"
read -p "Please input your  user of mysql:" mysqlrootuser
if [ "$mysqlrootuser" = "" ]; then
    echo "MySQL root user can't be NULL!"
    exit 1
else
echo "==========================="
echo "Your user of mysql was:$mysqlrootuser"
echo "==========================="
fi

mysqlrootpwd=""
read -p "Please input your root password of mysql:" mysqlrootpwd
if [ "$mysqlrootpwd" = "" ]; then
    echo "MySQL root password can't be NULL!"
    exit 1
else
echo "==========================="
echo "Your root password of mysql was:$mysqlrootpwd"
echo "==========================="
fi

yum install  libcurl-devel net-snmp-devel   -y 

useradd zabbix

/etc/init.d/mysqld restart


wget -SO fping-3.8.tar.gz   http://git.oschina.net/wujingke/linuxserver/raw/master/software/fping-3.8.tar.gz
tar fxz fping-3.8.tar.gz
cd  fping-3.8
./configure && make && make install

mysql -e "create database zabbix character set utf8;"  -u$mysqlrootuser  -p$mysqlrootpwd
mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'cpisisok';" -u$mysqlrootuser  -p$mysqlrootpwd
mkdir -p /www/zabbix

wget -SO zabbix-2.2.0.tar.gz  http://git.oschina.net/wujingke/linuxserver/raw/master/software/zabbix-2.2.0.tar.gz
tar fxz zabbix-2.2.0.tar.gz
cd zabbix-2.2.0
LIBS=
CFLAGS=
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl && make install
#####MYSQL########
mysql -uzabbix -p'cpisisok' zabbix < database/mysql/schema.sql
mysql -uzabbix -p'cpisisok' zabbix < database/mysql/images.sql
mysql -uzabbix -p'cpisisok' zabbix < database/mysql/data.sql
####Zabbix#######
    sed -i 's/^DBUser=.*$/DBUser=zabbix/g' /usr/local/zabbix/etc/zabbix_server.conf
    sed -i 's/^.*DBPassword=.*$/DBPassword=cpisisok/g' /usr/local/zabbix/etc/zabbix_server.conf
    cp -r frontends/php /www/zabbix
    cp misc/init.d/fedora/core/zabbix_server /etc/init.d/
    cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
    sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_server
    sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_agentd
    sed -i '446s/#//g' /usr/local/zabbix/etc/zabbix_server.conf
    sed -i  's/# StartSNMPTrapper=0/StartSNMPTrapper=1/g' /usr/local/zabbix/etc/zabbix_server.conf

#####SERVERICES####
cat >> /etc/services <<EOF
    zabbix-agent 10050/tcp Zabbix Agent
    zabbix-agent 10050/udp Zabbix Agent
    zabbix-trapper 10051/tcp Zabbix Trapper
    zabbix-trapper 10051/udp Zabbix Trapper
EOF
####PHP######
    sed -i 's/^\(.*\)date.timezone =.*$/date.timezone = Asia\/Shanghai/g' /etc/php.ini
    sed -i 's/^\(.*\)post_max_size =.*$/post_max_size = 16M/g' /etc/php.ini
    sed -i 's/^\(.*\)max_execution_time =.*$/max_execution_time = 300/g' /etc/php.ini
    sed -i 's/^\(.*\)max_input_time =.*$/max_input_time = 300/g' /etc/php.ini
#####START#####
/etc/init.d/zabbix_server start
/etc/init.d/zabbix_agentd start

cd /www/zabbix/php/fonts
wget -SO DejaVuSans.ttf  http://git.oschina.net/wujingke/linuxserver/raw/master/DejaVuSans.ttf
cd .. 

